package com.joysuch.wwyt.edu.schedule;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.joysuch.wwyt.core.support.Context;
import com.joysuch.wwyt.edu.entity.EduExamCourse;
import com.joysuch.wwyt.edu.entity.EduTrainningRecord;
import com.joysuch.wwyt.edu.entity.EduTrainningRecordCourse;
import com.joysuch.wwyt.edu.enums.TrainingFrom;
import com.joysuch.wwyt.edu.enums.TrainingOpenFace;
import com.joysuch.wwyt.edu.enums.TrainingQualifiedStandard;
import com.joysuch.wwyt.edu.enums.TrainingUserSignStatus;
import com.joysuch.wwyt.edu.mapper.EduExamMapper;
import com.joysuch.wwyt.edu.mapper.EduTrainningRecordCourseMapper;
import com.joysuch.wwyt.edu.mapper.EduTrainningRecordMapper;
import com.joysuch.wwyt.edu.mapper.EduTrainningRecordUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class TrainingDataSchedule {

    @Autowired
    private EduTrainningRecordMapper eduTrainningRecordMapper;
    @Autowired
    private EduExamMapper eduExamMapper;
    @Autowired
    private EduTrainningRecordCourseMapper eduTrainningRecordCourseMapper;

    //    @Scheduled(cron = "0/10 * * * * ?")
    @Scheduled(cron = "0 0/35 * * * ? ")
    public void run() {
        if (eduTrainningRecordMapper.selectSole() > 0) {
            log.info("结束任务");
            return;
        }
        List<EduTrainningRecord> eduTrainningRecords = eduTrainningRecordMapper
                .selectList(new LambdaQueryWrapper<EduTrainningRecord>()
                .eq(EduTrainningRecord::getDeleteFlag, "0"));
        eduTrainningRecords.forEach(trainningRecord -> {
            //关联考试
            if (!ObjectUtils.isEmpty(trainningRecord.getExamId())) {
                //考试关联课程
                if (eduExamMapper.selectExamCourse(trainningRecord.getExamId()) > 0) {
                    //生成线上培训 合格标准学完课程+考试通过
                    trainningRecord.setTrainingForm(TrainingFrom.OFFLINE.getId());
                    trainningRecord.setTrainingQualifiedStandard(TrainingQualifiedStandard.FINISH_COURSE_EXAM.getId());
                    trainningRecord.setHasExam("Y");
                    if (StringUtils.isEmpty(trainningRecord.getHasCert())){
                        trainningRecord.setHasCert("N");
                    }
                    eduTrainningRecordMapper.updateById(trainningRecord);
                    List<EduExamCourse> eduExamCourses = eduExamMapper.selectExamCourseList(trainningRecord.getExamId());
                    eduExamCourses.forEach(eduExamCourse -> {
                        //  取消考试关联课程表，转移到培训关联课程表中
                        EduTrainningRecordCourse eduTrainningRecordCourse = new EduTrainningRecordCourse();
//                        eduTrainningRecordCourse.setId(eduExamCourse.getId());
                        eduTrainningRecordCourse.setTrainningRecordId(eduExamCourse.getExamId());
                        eduTrainningRecordCourse.setCourseId(eduExamCourse.getCourseId());
                        eduTrainningRecordCourse.setCreateTime(new Date());
                        eduTrainningRecordCourse.setCreateBy(Context.getCurrentUserId());
                        eduTrainningRecordCourseMapper.insert(eduTrainningRecordCourse);
                    });
                } else {
                    //考试中没有关联课程
                    //生成线下培训 合格标准考试通过
                    trainningRecord.setTrainingForm(TrainingFrom.ONLINE.getId());
                    trainningRecord.setHasExam("Y");
                    if (StringUtils.isEmpty(trainningRecord.getHasCert())){
                        trainningRecord.setHasCert("N");
                    }
                    trainningRecord.setTrainingQualifiedStandard(TrainingQualifiedStandard.PASS_EXAM.getId());
                    eduTrainningRecordMapper.updateById(trainningRecord);
                }

            } else {
                //没有关联考试
                //生成线下培训 合格标准考试通过
                trainningRecord.setHasExam("N");
                if (StringUtils.isEmpty(trainningRecord.getHasCert())){
                    trainningRecord.setHasCert("N");
                }
                trainningRecord.setTrainingForm(TrainingFrom.ONLINE.getId());
                trainningRecord.setTrainingQualifiedStandard(TrainingQualifiedStandard.MANUAL_ENTRY.getId());
                eduTrainningRecordMapper.updateById(trainningRecord);
            }
        });
        EduTrainningRecord eduTrainningRecord = new EduTrainningRecord();
        eduTrainningRecord.setCode("WY00001");
        eduTrainningRecord.setDeleteFlag("1");
        eduTrainningRecordMapper.insert(eduTrainningRecord);
    }
}
